package com.dv.chat;


import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @Author DavidLoman
 * @create 2021/5/3 17:38
 */
public class UdpReceiveDemo01 {
    public static void main(String[] args) throws Exception{

        DatagramSocket datagramSocket = new DatagramSocket(6666);
        while (true){
            //准备接受包裹
            byte[] container = new byte[1024];
            DatagramPacket packet = new DatagramPacket(container, 0, container.length);
            datagramSocket.receive(packet);//阻塞式接收包裹

            byte[] data = packet.getData();
            String receiveData = new String(data, 0, packet.getLength());
            System.out.println(receiveData);
            if (receiveData.equals("bye")){
                break;
            }
        }
        datagramSocket.close();
    }
}
